package hz.mobileproject.pda.ui.view;


import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import hz.mobileproject.pda.R;

/**
 * Created by Lc on 2017/12/26.
 */

public class ContentSummaryOtherDialog extends BaseCustomDialog implements TextWatcher {
    @ViewInject(R.id.et_content_summary)
    private EditText et_content_summary;
    @ViewInject(R.id.tv_title)
    private TextView tv_title;
    @ViewInject(R.id.tv_curLength)
    private TextView tv_curLength;
    @ViewInject(R.id.tv_maxLength)
    private TextView tv_maxLength;
    @ViewInject(R.id.ll_limit)
    private LinearLayout ll_limit;
    @ViewInject(R.id.ll_content)
    private LinearLayout ll_content;


    //输入内容是否可以为空   默认不可以
    private boolean isContentCanNull = false;


    //字数限制，默认长度25
    private int contentMaxLength = 25;
    //最小长度限制   默认不限制
    private int contentMinLength = -1;
    private ContentSummaryOtherDialog.OnClickListener onClickListener;
    private boolean isVisable = true;

    public ContentSummaryOtherDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int bindView() {
        return R.layout.dialog_content_summary_other;
    }

    @Override
    protected void initData() {
        tv_maxLength.setText(contentMaxLength + "");
        tv_curLength.setText(et_content_summary.getText().toString().length() + "");
        et_content_summary.addTextChangedListener(this);
    }

    @Event({R.id.btn_positive, R.id.btn_nagetive})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.btn_positive:
                if(isVisable){
                    if (!isContentCanNull && StringUtils.isEmpty(et_content_summary.getText())) {
                        ToastUtils.showLong("输入不能为空");
                        return;
                    }
                    if(et_content_summary.getText().length()<contentMinLength){
                        ToastUtils.showLong("最少输入"+contentMinLength+"个字");
                        return;
                    }
                }
                this.dismiss();
                if (onClickListener != null) {
                    onClickListener.onPositiveClick(et_content_summary.getText().toString());
                }
                break;
            case R.id.btn_nagetive:
                this.dismiss();
                if (onClickListener != null) {
                    onClickListener.onNagetiveClick();
                }
                break;
        }
    }


    public ContentSummaryOtherDialog setContentMaxLength(int contentMaxLength) {
        this.contentMaxLength = contentMaxLength;
        return this;
    }

    public ContentSummaryOtherDialog setContentMinLength(int contentMinLength) {
        this.contentMinLength = contentMinLength;
        return this;
    }

    public ContentSummaryOtherDialog setTitle(String title) {
        this.tv_title.setText(title);
        return this;
    }

    public ContentSummaryOtherDialog setMessage(String message) {
        this.et_content_summary.setHint(message);
        return this;
    }

    public ContentSummaryOtherDialog setContent(String content) {
        this.et_content_summary.setText(content);
        et_content_summary.setSelection(et_content_summary.getText().toString().length());
        return this;
    }

    public ContentSummaryOtherDialog setContentCanNull(boolean contentCanNull) {
        isContentCanNull = contentCanNull;
        return this;
    }


    public ContentSummaryOtherDialog setContentVisable(boolean isVisable) {
        this.isVisable = isVisable;
        if (isVisable) {
            ll_content.setVisibility(View.VISIBLE);
            ll_limit.setVisibility(View.VISIBLE);
        } else {
            ll_content.setVisibility(View.GONE);
            ll_limit.setVisibility(View.GONE);
        }
        return this;
    }


    public interface OnClickListener {
        void onPositiveClick(String s);

        void onNagetiveClick();
    }

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public ContentSummaryOtherDialog setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (et_content_summary.getText().length() > contentMaxLength) {
            String str = et_content_summary.getText().toString().substring(0, contentMaxLength);
            et_content_summary.setText(str);
            et_content_summary.setSelection(et_content_summary.getText().toString().length());
        }
        tv_curLength.setText(et_content_summary.getText().toString().length() + "");
    }
}
